<!-- 首页楼层组件：有两层，可以复用 -->
<template>
    <!--楼层-->
    <div class="floor">
        <div class="py-container">
            <div class="title clearfix">
                <h3 class="fl">{{ floorData.name }}</h3>
                <div class="fr">
                    <ul class="nav-tabs clearfix">
                        <li class="active" v-for="(navListItem, index) in floorData.navList" :key="index">
                            <a href="#tab1" data-toggle="tab">{{ navListItem.text }}</a>
                        </li>
                    </ul>
                </div>
            </div>
            <div class="tab-content">
                <div class="tab-pane">
                    <div class="floor-1">
                        <div class="blockgary">
                            <ul class="jd-list">
                                <li v-for="(keywordsItem, index) in floorData.keywords" :key="index">
                                    {{ keywordsItem }}
                                </li>
                            </ul>
                            <img :src="floorData.imgUrl" />
                        </div>
                        <!-- 等下写 -->
                        <div class="floorBanner">
                            <div class="swiper-container" ref="floorSwiper" id="floor1Swiper">
                                <!-- 轮播图 -->
                                <div class="swiper-wrapper">
                                    <div class="swiper-slide" v-for="(carouselListItem) in floorData.carouselList"
                                        :key="carouselListItem.id">
                                        <img :src="carouselListItem.imgUrl">
                                    </div>
                                </div>
                                <!-- 如果需要分页器 -->
                                <div class="swiper-pagination"></div>

                                <!-- 如果需要导航按钮 -->
                                <div class="swiper-button-prev"></div>
                                <div class="swiper-button-next"></div>
                            </div>
                        </div>
                        <div class="split">
                            <span class="floor-x-line"></span>
                            <div class="floor-conver-pit"
                                v-for="(recommendListItem, recommendListIndex) in floorData.recommendList.slice(0, 2)"
                                :key="recommendListIndex">
                                <img :src="recommendListItem" />
                            </div>
                        </div>
                        <div class="split center">
                            <img :src="floorData.bigImg" />
                        </div>
                        <div class="split">
                            <span class="floor-x-line"></span>
                            <div class="floor-conver-pit"
                                v-for="(recommendListItem, recommendListIndex) in floorData.recommendList.slice(2, 4)"
                                :key="recommendListIndex">
                                <img :src="recommendListItem" />
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import Swiper, { Pagination, Navigation, Autoplay } from 'swiper'
Swiper.use([Pagination, Navigation, Autoplay])
export default {
    name: 'FloorIndex',
    props: {
        floorData: {
            type: [Object, Array],
            required: true
        }
    },
    mounted() {
        // 首次加载时尝试初始化
        this.initSwiper();
    },
    watch: {
        floorData: {
        //immediate: true  立即监听：不管你数据有没有变化，我是来立即监听一次
        ////为什么watch监听不到floorData？ 因为这个数据出来没有发生过变化
        // （floorData数据是父亲home.vue给的，父亲给的时候就是一个对象，对象里面该有的数据已经都有的）
            immediate: true, // 立即触发监听
            handler() {
                this.$nextTick(() => {
                    this.initSwiper();
                });
            }
        }
    },
    methods: { 
        initSwiper() {
            if (!this.mySwiper && this.floorData.carouselList?.length > 0) {
                this.mySwiper = new Swiper(this.$refs.floorSwiper, {
                    loop: true,
                    slidesPerView: 1,
                    autoplay: {
                        delay: 3000,
                        disableOnInteraction: false,
                    },
                    pagination: {
                        el: '.swiper-pagination',
                        clickable: true,
                    },
                    navigation: {
                        nextEl: '.swiper-button-next',
                        prevEl: '.swiper-button-prev',
                    },
                });
            }
        }
    }
}
</script>
<style lang='less' scoped>
.floor {
    margin-top: 15px;

    .py-container {
        width: 1200px;
        margin: 0 auto;

        .title {
            .fl {
                float: left;
                color: #c81623;
                font-size: 20px;
                line-height: 30px;
                margin: 9px 0;
                font-weight: 700;
            }

            .fr {
                float: right;

                .nav-tabs {
                    margin: 10px 0 0;
                    display: inline-block;

                    li {
                        float: left;
                        line-height: 18px;

                        a {
                            padding-top: 1px;
                            font-weight: 400;
                            background-color: #fff;

                            &::after {
                                content: "|";
                                padding: 0 10px;
                            }
                        }

                        &:nth-child(7) {
                            a {
                                &::after {
                                    content: "";
                                }
                            }
                        }

                        &.active {
                            a {
                                color: #e1251b;
                            }
                        }
                    }
                }
            }
        }

        .tab-content {
            border-top: 2px solid #c81623;
            border-bottom: 1px solid #e4e4e4;

            .tab-pane {
                .floor-1 {
                    height: 360px;
                    display: flex;

                    .blockgary {
                        width: 210px;
                        height: 100%;
                        background: #f7f7f7;

                        .jd-list {
                            padding: 15px 0;
                            overflow: hidden;

                            li {
                                list-style-type: none;
                                float: left;
                                width: 40%;
                                margin: 0 10px;
                                border-bottom: 1px solid #e4e4e4;
                                text-align: center;
                                line-height: 26px;
                            }
                        }

                        img {
                            width: 100%;
                        }
                    }

                    .floorBanner {
                        width: 330px;
                        height: 100%;
                    }

                    .split {
                        width: 220px;
                        height: 100%;
                        position: relative;

                        .floor-x-line {
                            position: absolute;
                            background: #e4e4e4;
                            width: 220px;
                            height: 1px;
                            top: 180px;
                        }

                        .floor-conver-pit {
                            width: 100%;
                            height: 50%;

                            img {
                                width: 100%;
                                height: 100%;
                                transition: all 400ms;

                                &:hover {
                                    opacity: 0.8;
                                }
                            }
                        }
                    }

                    .center {
                        border: 1px solid #e4e4e4;
                    }
                }
            }
        }
    }
}
</style>